<?php if (!defined('PmWiki')) exit();

/* foxdelete.php  Copyright Hans Bracker 2008.
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
*/
$RecipeInfo['FoxDelete']['Version'] = '2008-07-12';

# add action foxdelete
$HandleActions['foxdelete'] = 'FoxHandleDelete';

function FoxHandleDelete($pagename) {
	global $FoxAuth, $ChangeSummary, $Now, $EditFunctions;

	# delete permission set by $FoxAuth
	$page = RetrieveAuthPage($pagename, $FoxAuth, true);
	if (FoxPagePermission($pagename, 'delete', $pagename, $page, $fx) == false)
		FoxAbort($pagename, "");
	if (!$page) FoxAbort($pagename, "ERROR: Cannot read $page! ");
	$new = $page;

	$args = FoxRequestArgs(); //fetch GET or POST arguments
	$key = $args['linekey'];  # Retrieve the line-key
	if ($key=='') FoxAbort($pagename, "ERROR: Delete action was unsuccessful! Linekey identifier is missing. ");

	# trim text and add newline so the following regexes also work for the last line
	$text = rtrim($page['text'])."\n";
	$old = $text;
	# Remove the line containing the delete statement with the provided line-key (if it exists)
	$text = preg_replace('/^.*\\{\\[foxdelline(| button)? '.$key.' .*\\n/m',"",$text);
	# Remove the range containing the delrange statement with the provided line-key (if it exists)
	$text = preg_replace('/#foxbegin '.$key.'#.*?\\{\\[foxdelrange(| ?button) '.$key.' .*?#foxend '.$key.'#.*?\n/s',"",$text);
	if($old==$text)
		FoxAbort($pagename, "ERROR: Delete action was unsuccessful! ");
	# Remove the added newline character (or any whitespace from the end)
	$text = rtrim($text);
	unset($EditFunctions['EditTemplate'], $EditFunctions['RestorePage'],
			$EditFunctions['AutoCreateTargets'], $EditFunctions['PreviewPage']); 
	# save page
	$new['text'] = $text;
	$new['csum'] = $ChangeSummary;
  	if ($ChangeSummary) $new["csum:$Now"] = $ChangeSummary;
	UpdatePage($pagename, $page, $new, $EditFunctions);
	# set up page redirection, cater for deletelink ($_GET)
	if(@$args['base']) $pagename = $args['base'];
	Redirect($pagename);
} //}}}


Markup('foxdelete','directives','/\{\[foxdel(line|range) ?(|button)\\s*(.*?)\\s*\]}/e',
		"Keep(FoxDeleteMarkup(\$pagename, '$1', '$2', PSS('$3') ))");
# Creates the HTML code for delete links {[foxdelline]}, {[foxdelrange]}
# and delete buttons {[foxdelline button]} and {[foxdelrange button]}
function FoxDeleteMarkup($pagename, $range, $type, $args) {	
	global $ScriptUrl, $EnablePathInfo, $EnableFoxDeleteMsg, $FoxDeleteMsg, $FoxDeleteSummaryMsg;
	SDV($EnableFoxDeleteMsg, false); //set to true to enable post delete confirmation
	SDV($FoxDeleteMsg, '$[Please confirm: Do you want to delete this post?]');
	SDV($FoxDeleteSummaryMsg, '$[Post deleted]'); //if you don't want a summary message, set it to ''

	$args = ParseArgs($args);
	$args[''] = (array)@$args[''];	
	$key = array_shift($args['']);
	$target = (isset($args['target'])) ? $args['target'] : array_shift($args['']);
	$label = (isset($args['label'])) ? $args['label'] : array_shift($args['']);
	//tooltip for delete link
	if (isset($args['tooltip'])) $tooltip = "title='{$args['tooltip']}'";
	else if (isset($args['title'])) $tooltip = "title='{$args['title']}'";
	else $tooltip = '';
	
	if ($target=="") $target = $pagename;
	$TargetPageUrl = PUE(($EnablePathInfo)
			? "$ScriptUrl/$target"
         : "$ScriptUrl?n=$target");

	# javascript delete message dialogue
	if($EnableFoxDeleteMsg==true)
		$onclick = "onclick='cf=confirm(\"{$FoxDeleteMsg}\");if(cf==true)return true;else return false;'";
	else $onclick = "";
	if($label=='') $label = '$[Delete]';
	if($type=='button') {
	# delete button
	$out = FmtPageName("<form class='foxdelbutton' action='{$TargetPageUrl}' method='post'><div>
			<input type='hidden' name='n' value='$target' />
			<input type='hidden' name='base' value='$pagename' />
			<input type='hidden' name='action' value='foxdelete' />
			<input type='hidden' name='csum' value='{$FoxDeleteSummaryMsg}' />
			<input type='hidden' name='linekey' value='$key' />
			<input type='submit' name='doit' value='{$label}' class='inputbutton' {$onclick}/>
			</div></form>", $target);
	}
	else {
		# delete link which works with and without javascript:
		$out = FmtPageName("<a class='foxdellink' $tooltip href='{$TargetPageUrl}?action=foxdelete&amp;linekey=$key&amp;base=$pagename&amp;csum={$FoxDeleteSummaryMsg}' rel='nofollow'
			{$onclick}>{$label}</a>",$target);
	}
	return $out;
} //}}}
